#!/bin/bash
PATH="$PATH:/sbin:/home/pi/siweather/scripts"

scripts_root="/home/pi/siweather/scripts"
pics_root="/home/pi/web/pics"

maestro_dir="/home/pi/siweather/maestro"

connected=`$maestro_dir/./UscCmd --list 2>&1 | grep Maestro | awk '{ print $1}'`
if [ $connected -lt 1 ]; then echo "No Maestro servo controller found!"; exit 1; fi


if [ ! -d $pics_root"/large" ]; then mkdir $pics_root"/large"; fi
if [ ! -d $pics_root"/medium" ]; then mkdir $pics_root"/medium"; fi
if [ ! -d $pics_root"/small" ]; then mkdir $pics_root"/small"; fi

font_size="16"
font_colour="black"

daytime='1'
index_file=/usr/local/var/wview/img/index.html
if [ -a $index_file ]
then
	daytime=`fgrep "is_it_day_time" $index_file | awk '{print $2}'`
fi

if [ 0 -eq $daytime ]
then
    echo "It's night time"
	cam_power_off
	exit 0
fi

echo `date`

check_exists=`ps -e | grep take_photos | wc | awk '{ print $1 }'`
echo $check_exists
if [ $check_exists -gt 3 ]
then
	echo "Already running"
	exit 0
fi

# timeout=360
# set_timer() { (
#   sleep $timeout
#   pid_exists=`ps -e | grep $$`
#   if [ -n "$pid_exists" ]
#   then
#     echo "Runtime exceeded "$timeout" seconds !!"
# 
#     kill $$
#   fi
# )& }
# 
# set_timer

cam_power_on
sleep 1
if [ $? -ne 0 ]; then echo "Camera power on failed!"; exit 1; fi

gphoto2 --set-config capture=on
if [ $? -ne 0 ]; then echo "Camera setting failed!"; exit 1; fi
gphoto2 --set-config imagesize="Medium 2"
if [ $? -ne 0 ]; then echo "Camera setting failed!"; exit 1; fi
gphoto2 --set-config capturetarget=0
if [ $? -ne 0 ]; then echo "Camera setting failed!"; exit 1; fi

angles=( 70 120 190 270 )
angles=( 70 110 150 190 230 270 )
angles=( 340 20 60 100 140 180 )
angles=( 340 20 100 180 )

for i in {0..3} # MUST MAKE THIS 0..<number_of_pictures - 1> !!!
do
	echo " "
	echo "Photo number "$(($i+1))
	echo " "
	
	turn_servo ${angles[i]}
	
	reply=1
	while [ $reply -ne 0 ]; do
		gphoto2 --capture-image-and-download --force-overwrite --filename $pics_root/large/im"$i".jpg
		reply=$?
		echo "GPhoto reply: "$reply
		sleep 3
	done

	convert $pics_root/large/im"$i".jpg -flip -flop $pics_root/large/im"$i".jpg
	chmod 644 $pics_root/large/*

	convert $pics_root/large/im$i.jpg -scale 640x480 $pics_root/medium/im$i.jpg
# 	convert $pics_root/medium/im$i.jpg -pointsize $font_size -fill $font_colour -annotate +10+20 "`date`" $pics_root/medium/im$i.jpg

	convert $pics_root/large/im$i.jpg -scale 320x240 $pics_root/small/im$i.jpg
	convert $pics_root/small/im$i.jpg -pointsize $font_size -fill $font_colour -annotate +10+20 "`date`" $pics_root/small/im$i.jpg
done

gphoto2 --set-config capture=off
sleep 1


cam_power_off
if [ $? -ne 0 ]; then echo "Camera power off failed!"; exit 1; fi

stamp=`date +%y-%m-%d_%H.%M`

# Exit here if you don't want a panorama generated
exit 0

cd $pics_root/medium
rm pano_*.jpg
killall autostitch.exe
killall autostitch_pano
sleep 2
DISPLAY=:0 $scripts_root/autostitch_pano im*
convert $pics_root/medium/pano.jpg -pointsize $font_size -fill $font_colour -annotate +90+90 "`date`" $pics_root/medium/pano_$stamp.jpg
convert $pics_root/medium/pano_$stamp.jpg -pointsize $font_size -fill $font_colour -annotate +950+90 "`date`" $pics_root/medium/pano_$stamp.jpg
echo $stamp  > /home/tom/web/pano_time.txt
echo "var pano_file = \"web/pics/medium/pano_"$stamp".jpg\";" > /home/tom/web/timestamp.inc
exit 0

